package com.sxpi.controller;

import com.sxpi.common.result.Result;
import com.sxpi.model.entity.CostumesVo;
import com.sxpi.model.vo.StoresVo;
import com.sxpi.service.CostumesService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/costumes")
public class CostumesController {
    @Resource
    private CostumesService costumesService;

    /**
     * 根据分类ID获取服饰
     */
    @GetMapping("/getByCategoryId/{id}")
    public Result<List<CostumesVo>> getByCategoryId(@PathVariable Long id,
                                                    @RequestParam(required = false) Long userId) {
        List<CostumesVo> costumes = costumesService.getByCategoryId(id,userId);
        return Result.ok(costumes);
    }

    /**
     * 根据子级id查询到相关店铺信息
     */
    @GetMapping("/getStores/{id}")
    public Result<List<StoresVo>> getStores(@PathVariable Long id) {
        List<StoresVo> store=costumesService.getStores(id);
        return Result.ok(store);
    }

    /**
     * 根据名称搜索服装
     * @param name 服装名称
     * @return 匹配的服装列表
     */
    @GetMapping("/search")
    public Result<List<CostumesVo>> searchByName(@RequestParam String name) {
        List<CostumesVo> costumes = costumesService.searchByName(name);
        return Result.ok(costumes);
    }

    /**
     * 获取热门服装
     * @param pageNo 页码（可选）
     * @param pageSize 每页大小（可选）
     * @param imageCount 每个服装返回的图片数量（可选）
     * @return 热门服装信息
     */
    @GetMapping("/getHotCostumes")
    public Result<Object> getHotCostumes(
            @RequestParam(required = false) Integer pageNo,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) Integer imageCount) {
        if (pageNo != null && pageSize != null) {
            // 返回分页的热门服装列表
            List<Map<String, Object>> hotCostumes = costumesService.getHotCostumesWithPage(pageNo, pageSize, imageCount);
            return Result.ok(hotCostumes);
        } else {
            // 返回销量最高的单个服装
            Map<String, Object> topCostume = costumesService.getTopCostume();
            return Result.ok(topCostume);
        }
    }

    @PostMapping("/addImages/{id}")
    public Result<Boolean> addImages(@RequestParam("files") List<MultipartFile> files,
                                          @PathVariable Long id ){
        log.info("files:{}",files);
        return costumesService.addImages(id,files);
    }

}
